package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.tanhua.common.pojo.Question;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.api.VisitorsApi;
import com.tanhua.dubbo.server.pojo.Visitors;
import com.tanhua.server.vo.TodayBest;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: my-tanhua
 * @description:
 * @author: xuejingjing
 * @create: 2021-10-16 10:39
 **/
public class TanHuaService {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private QuestionService questionService;


    @Reference(version = "1.0.0")
    private HuanXinApi huanXinApi;

    //在一个实现中，调用远程实现的接口
    @Reference(version = "1.0.0")
    private VisitorsApi visitorsApi;


//包括当前登录的用户和来访的用户
    public TodayBest queryUserInfo(Long userId) {
        UserInfo userInfo = userInfoService.queryUserInfoByUserId(userId);
        if(ObjectUtil.isEmpty(userInfo)){
            return null;
        }
        TodayBest todayBest = new TodayBest();
        todayBest.setId(userId);
        todayBest.setAge(userInfo.getAge());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setGender(userInfo.getSex().name().toLowerCase());
        //当前登录的用户
        Long toUserId = UserThreadLocal.get().getId();
        todayBest.setFateValue(recommendUserService.queryScore(userId,toUserId).longValue());
        todayBest.setTags(Convert.toStrArray(StrUtil.split(userInfo.getTags(),",")));

        /**
         * 记录来访的用户
         */
        visitorsApi.queryMyVisitor(userId, toUserId, "个人主页");

        return todayBest;
    }

    /**
     * 查询陌生人问题
     * @param userId
     * @return
     */
    public String queryQuestion(Long userId) {
        Question question = questionService.queryQuestion(userId);
        if(ObjectUtil.isEmpty(question)){
            return "你的爱好是什么？";
        }
        return question.getTxt();
    }

    /**
     * 回复问题   //
     * @param userId
     * @param reply
     * @return
     */
    public Boolean replyQuestion(Long userId, String reply) {
        User user = UserThreadLocal.get();
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(user.getId());

        //构建消息内容
        Map<String, Object> msg = new HashMap<>();
        msg.put("userId", user.getId());
        msg.put("huanXinId", "HX_" + user.getId());
        msg.put("nickname", userInfo.getNickName());
        msg.put("strangerQuestion", this.queryQuestion(userId));
        msg.put("reply", reply);

        //发送环信消息
        return this.huanXinApi.sendMsgFromAdmin("HX_" + userId,
                HuanXinMessageType.TXT, JSONUtil.toJsonStr(msg));
    }


}